/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fiap.dao;

import br.com.fiap.vo.CursoVO;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Fabianoss
 */
public class CursoDAO {
    
    private CursoVO CursoVO;
    private String query;
    private Connection conn;
    private Statement stmt;
    private PreparedStatement pstmt;
    private ResultSet result;
    private FabricaConexao fb;
    
    public CursoDAO(){
         fb = FabricaConexao.getInstance();       
    }
    
    public void Salvar(CursoVO CursoVO){
        this.query = "INSERT INTO `avaliacao1`.`curso` (`nome`)VALUES(?);";
          try {   
            this.pstmt = fb.getConnection().prepareStatement(query);
            this.pstmt.setString(1, CursoVO.getNome());            
            this.pstmt.execute(); 
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Erro ao inserir curso.: "+ e.getMessage());
        }finally{
              fb.closeConnection(pstmt, null, null);
        }
    }
    
    public void update(CursoVO CursoVO){
        this.query = "UPDATE `avaliacao1`.`curso`SET `nome` = ? WHERE idcurso = ?;";
          try {
            this.pstmt = fb.getConnection().prepareStatement(query);
            this.pstmt.setString(1, CursoVO.getNome());
            this.pstmt.setInt(2, CursoVO.getIdCurso());
            pstmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Erro ao inserir Curso.: "+ e.getMessage());
        }finally{
              fb.closeConnection(pstmt, null, null);
        }
    }
    
    public void delete(CursoVO CursoVO){        
       this.query = "DELETE FROM `avaliacao1`.`curso`WHERE idcurso = ?;";
          try {
            this.pstmt = fb.getConnection().prepareStatement(query);
            this.pstmt.setInt(1, CursoVO.getIdCurso());
            pstmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Erro ao deletar Curso.: "+ e.getMessage());
        }finally{
              fb.closeConnection(pstmt, null, null);
        }
        
    }
    
    public List<CursoVO> listarCursosVO(){
        List<CursoVO> lista = new ArrayList<CursoVO>();
        this.query = "SELECT * FROM `avaliacao1`.`curso`;";
          try {
             this.stmt = fb.getConnection().createStatement();
             this.result = stmt.executeQuery(query);
             
             while(result.next()){
                 CursoVO vo = new CursoVO();
                 vo.setIdCurso(result.getInt("idcurso"));
                 System.out.print(vo.getIdCurso());
                 vo.setNome(result.getString("nome"));
                 lista.add(vo);
             }            
            
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Erro ao deletar Usuario.: "+ e.getMessage());
        }finally{
              fb.closeConnection(null, stmt, result);
        }
        return lista;  
    }
    
     public CursoVO getCursoVOById(int id){
        
        this.query = "SELECT * FROM `avaliacao1`.`curso` where idcurso = ?;";
        CursoVO vo = new CursoVO(); 
        try {
             this.pstmt = fb.getConnection().prepareCall(query);
             this.pstmt.setInt(1, id);
             this.result = pstmt.executeQuery();
             
             while(result.next()){
                
                 vo.setIdCurso(result.getInt("idcurso"));
                 vo.setNome(result.getString("nome"));
                 
             }            
            
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Erro ao selecionar Usuario.: "+ e.getMessage());
        }finally{
              fb.closeConnection(pstmt, null, result);
        }
        return vo;  
    }
    
}

    